﻿using System;

namespace LockerRack.Helpers
{
    //<!-- Date Format is YYYY/MM/DD HH:MM:SS -->
    public static class DateToStringFormatterExtension
    {
        public const string DateSeparator = "/";
        public const string DateTimeSeparator = " ";
        public const string TimeSeparator = ":";
        public const string Separators = "/ :";
        public static string ToiBwaveMobileString(this DateTime date)
        {
            var ret = string.Format("{0}/{1}/{2} {3}:{4}:{5}",
                                    date.Year.ToString("0000"),
                                    date.Month.ToString("00"),
                                    date.Day.ToString("00"),
                                    date.Hour.ToString("00"),
                                    date.Minute.ToString("00"),
                                    date.Second.ToString("00"));

            return ret;
        }
    }

    public static class StringToDateFormatterExtension
    {
        public static DateTime ToiBwaveMobileDate(this string dateString, out Exception ex)
        {
            ex = null;
            DateTime retDate;

            if (string.IsNullOrEmpty(dateString))
            {
                ex = new Exception("DateTime Parsing: Source string is empty");
                return DateTime.MinValue;
            }

            try
            {
                var dateTimeArr = dateString.Split(DateToStringFormatterExtension.DateTimeSeparator.ToCharArray(),
                                                   StringSplitOptions.RemoveEmptyEntries);

                if (dateTimeArr.Length != 2)
                {
                    ex = new Exception("DateTime Parsing: Can't split Date and Time parts; check date format.");
                    return DateTime.MinValue;
                }

                var dateArr = dateTimeArr[0].Split(DateToStringFormatterExtension.DateSeparator.ToCharArray(),
                                                StringSplitOptions.RemoveEmptyEntries);

                if (dateArr.Length != 3)
                {
                    ex = new Exception("DateTime Parsing: Date part has wrong length");
                    return DateTime.MinValue;
                }

                var timeArr = dateTimeArr[1].Split(DateToStringFormatterExtension.TimeSeparator.ToCharArray(),
                                                   StringSplitOptions.RemoveEmptyEntries);

                if (timeArr.Length != 3)
                {
                    ex = new Exception("DateTime Parsing: Time part has wrong length");
                    return DateTime.MinValue;
                }

                int year, month, day, counter = 0;
                bool succeded = true;
                succeded &= int.TryParse(dateArr[counter++], out year);
                succeded &= int.TryParse(dateArr[counter++], out month);
                succeded &= int.TryParse(dateArr[counter], out day);

                if (!succeded)
                {
                    ex = new Exception("DateTime Parsing: Date part has wrong symbols");
                    return DateTime.MinValue;
                }

                int hour, minute, second;
                counter = 0;
                succeded &= int.TryParse(timeArr[counter++], out hour);
                succeded &= int.TryParse(timeArr[counter++], out minute);
                succeded &= int.TryParse(timeArr[counter], out second);

                if (!succeded)
                {
                    ex = new Exception("DateTime Parsing: Time part has wrong symbols");
                    return DateTime.MinValue;
                }

                retDate = new DateTime(year, month, day, hour, minute, second);
            }
            catch (Exception e)
            {
                ex = e;
                retDate = DateTime.MinValue;
            }

            return retDate;
        }
    }
}
